

import SegmentedQueryScrollView from "../SegmentedQueryScrollView";

const { ccclass, property } = cc._decorator;

enum Direction {
    Up = -1,
    Down = 1,
}
@ccclass
export default class LT_SegmentedQueryScrollView extends SegmentedQueryScrollView {

    @property(cc.Node)
    visibleView: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:
    // public scrollToIndex: ScrollViewToIndex = null;

    private contentBpos: cc.Vec2 = null;

    onLoad() {
        super.onLoad();
        this.visibleView || (this.visibleView = this.scrollview.node);
        this.contentBpos = this._content.getPosition();
    }

    // onEnable() {
    //     super.onEnable();
    // }
    // onDisable() {
    //     super.onDisable();
    // }


    // start() {
    //     // super.start();
    //     // this.scrollToIndex = this.node.getComponent("ScrollViewToIndex");
    // }

    // update (dt) {}
    // init(args) {

    // }

    // onClickBack() {

    // } 
    getItemArr() {
        return this._itemArray;
    }
    setDataCount(count: number) {
        if (count == void 0) return false;
        this._dataCount = count;
        return true;
    }
    setDataByIndex(index = 0, maxIndex = 0, dataList: any[] = null, heightArr: number[] = []) {
        index = index < 0 ? 0 : index;
        let len = this._content.childrenCount;
        let startIndex = Math.ceil(index - len / 2);//- this.perLoad)
        startIndex = startIndex > maxIndex - len + 1 ? maxIndex - len + 1 : startIndex;
        startIndex = startIndex < 0 ? 0 : startIndex;
        //用於跳轉某個數據點時使用
        let y = this._content.y;
        this._content.height = 0 + this._layout.paddingTop + this._layout.paddingBottom;
        /** 子项数组 */
        this._itemArray = [];
        this._indexMaxMark = -1;
        for (let i = 0; i < len; i++) {
            let node = this._content.children[i];
            node.active = false;
            if (this._dir == Direction.Down) {
                this._itemArray.push(node);
            } else {
                this._itemArray.unshift(node);
            }
        }
        for (let i = 0; i < startIndex; i++) {
            this._refreshContentHeight(!dataList ? this.itemPrefab.data : ({ height: heightArr[dataList[i].type] }), i);
        }
        this._refreshUnUseChildData(true, startIndex);
        this._content.y = y;
    }
    _initAllItem() {

        if (!this._content) {
            this.onLoad()
        }
        /** 总是需要添加数据  */
        this._is_auto_add = true
        /** 初始化 */

        let len = this._content.children.length;
        for (let i = len - 1; i >= 0; i--) {
            let child = this._content.children[i];
            if (child) {
                this._put(child)
            }
        }
        this._content.height = 0 + this._layout.paddingTop + this._layout.paddingBottom
        this._content.y = this.contentBpos.y;
        /** 需要的子项数量  一页显示的+预加载的 */
        let need_count = 0
        need_count = Math.ceil((this._content.parent.height) / (this.itemPrefab.data.height + this._layout.spacingY)) + this.perLoad;

        /** 子项数组 */
        this._itemArray = []
        this._indexMaxMark = -1;


        for (let index = 0; index < need_count; index++) {
            let node = this._getCanUsedItem()
            node.parent = this._content
            node.active = false;
            node.index = index;
            if (this._dir == Direction.Down) {

                this._itemArray.push(node);
            } else {
                this._itemArray.unshift(node);
            }
        }


    }

    _ScrollEvent(evt = null) {
        let offset = this.detectionThreshold || 1 //每移动多少像素检测一次
        if (this._is_auto_add) {
            // 动态加载
            let chazhi = this._lastMoveY - this._content.y
            if (chazhi > offset) {//下滑

                // 计算最下面一个是否移出视图
                let bottom = this._getBottomItem()
                if (!bottom) return;
                //将View坐标 转换到content层进行比较
                let view = this.visibleView;
                let a = this.visibleView.convertToWorldSpaceAR(cc.v2())
                let b = this._content.convertToNodeSpaceAR(a)
                // (b.y < -(this._content.parent.height + bottom.height / 2) && this._dir == Direction.Down) || (b.y < -(bottom.height / 2) && this._dir == Direction.Up)
                if (((bottom.y + bottom.height / 2) < (b.y - view.height * (view.anchorY)))) {
                    // cc.log("下滑")
                    let top = this._getTopItem()
                    let topZorder = top.index;

                    if (topZorder <= 0) {
                        return
                    }
                    if (topZorder >= this._dataCount - 1) {
                        return
                    }
                    let order = topZorder - 1 * this._dir
                    //最下面的结点删掉
                    bottom = this._itemArray.pop();//
                    let node
                    //如果这个节点没有执行初始化,那么干掉这个重新创建一个新的
                    if (!this._itemIsInitByCallBack) {
                        this._put(bottom)
                        //创建一个节点 放在最上面
                        node = this._getCanUsedItem();
                        node.parent = this._content;
                    } else {
                        node = bottom
                    }
                    if (this._callback) {
                        this._callback(node, order, true);
                        this._refreshContentHeight(node, order)
                        this._getNewData(order, true)
                    }

                    node.y = top.y + this._layout.spacingY + node.height / 2 + top.height / 2
                    node.index = order;
                    this._itemArray.unshift(node);
                }
                this._lastMoveY = this._content.y
            } else if (chazhi < -offset) {//上滑
                // 计算最上面一个是否移出视图
                let top = this._getTopItem()
                if (!top) return;

                //将View坐标 转换到content层进行比较
                let view = this.visibleView;
                let a = this.visibleView.convertToWorldSpaceAR(cc.v2())
                let b = this._content.convertToNodeSpaceAR(a)
                if ((top.y - top.height / 2) > (b.y + view.height * (1 - view.anchorY))) {
                    // cc.log("上滑")
                    let bottom = this._getBottomItem()
                    let bottomOrder = bottom.index;
                    if (bottomOrder <= 0) {
                        return
                    }
                    if (bottomOrder >= this._dataCount - 1) {
                        return
                    }
                    let order = bottomOrder + 1 * this._dir
                    //最上面的结点删掉
                    top = this._itemArray.shift()
                    let node
                    //如果这个节点没有执行初始化,那么干掉这个重新创建一个新的
                    if (!this._itemIsInitByCallBack) {
                        this._put(top)
                        //创建一个节点 放在最上面
                        node = this._getCanUsedItem();
                        node.parent = this._content;
                    } else {
                        node = top
                    }

                    if (this._callback) {
                        this._callback(node, order, true);
                        this._refreshContentHeight(node, order)
                        this._getNewData(order)
                    }
                    node.y = bottom.y - this._layout.spacingY - node.height / 2 - bottom.height / 2
                    node.index = order;
                    this._itemArray.push(node);
                }
                this._lastMoveY = this._content.y
            }
        }
    }
}
